package org.epidroid.epidroid.androsoul.ui.contactlist;

import org.epidroid.epidroid.R;
import org.epidroid.epidroid.androsoul.engine.contacts.ContactGroup;
import org.epidroid.epidroid.androsoul.engine.service.IAndroSoulService;
import org.epidroid.epidroid.androsoul.ui.AndroSoulActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.EditText;

public class RenameGroupHandler {

	IAndroSoulService _service = null;
	AndroSoulActivity _context = null;

	public RenameGroupHandler(IAndroSoulService service, AndroSoulActivity context) {
		_service = service;
		_context = context;
	}

	/**
	 * Handle click on the button for rename a group
	 */
	public void showDialog(final ContactGroup group) {
		AlertDialog.Builder alert = new AlertDialog.Builder(_context);
		alert.setTitle(R.string.group_rename).setMessage(R.string.group_name_change);
		final EditText input = new EditText(_context);
		alert.setView(input);

		alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {

				if (input.getText().toString().equals("")) {
					// TODO: do not close the window when this happens
					_context.showToast(_context.getString(R.string.group_empty));
					return;
				}

				Boolean success = _service.renameGroup(group, input.getText()
						.toString());
				if (success) {
					_context.updateContactsListAdapter();
					_context.showToast(_context.getString(R.string.group_renamed));
					_context.saveContacts();
				} else {
					// TODO: do not close the window when this happens
					_context.showToast(_context.getString(R.string.group_name_exists));
				}
			}
		});
		alert.setNegativeButton(R.string.cancel, null);
		alert.show();
	}
}
